﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using CQIE.TES.IDAL;
using CQIE.TES.IService;
using CQIE.TES.Model;
using CQIE.TES.WEB.USER.Models;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.Service
{
    public class CourseService:ICourseService
    {
        private readonly ICourseDAL _dal;
        private readonly IClass_DAL classDal;
        public CourseService(ICourseDAL dal, IClass_DAL cDal)
        {
            this._dal = dal;
            this.classDal = cDal;
        }


        public List<Course> GetCoursesInfo(int page, int limit, string search, out int count)
        {
            return _dal.GetCoursesInfo(page - 1, limit, search, out count);
        }

        public List<Course> GetCoursesInfo(string search)
        {
            return _dal.GetCoursesInfo(search);
        }

        public bool Delete(int id)
        {
            // 失败重复删除5次 还是失败直接退出
            int num = 0;
            while (!_dal.Delete(id))
            {
                num++;
                if (num==4)
                {
                    return false;
                }
            }
            
            return true;
        }

        public List<Teacher> CourserTeachers(int courseID)
        {
            return _dal.CourserTeachers(courseID);
        }

        public void GiveOrRemoveCourseTeacher(List<int> teacherList, int courseID,int type)
        {
            
            _dal.GiveOrRemoveCourseTeacher(courseID,teacherList,type);
        }

        /// <summary>
        /// 获取所有没被冻结的课程
        /// </summary>
        /// <returns></returns>
        public List<Course> GetAllCourse() {
            return _dal.GetAllCourse();
        }

        public List<Course> GetCourseByStudentID(int studentId,int evaId)
        {
            var model = classDal.FindClassByStundentID(studentId);
            return _dal.GetClassCourse(model.ID, evaId);
        }

        

        public List<Course> GetClassCourse(int classId, int studyYearId, out int count, int pageNum, int limit)
        {
            return _dal.GetClassCourse(classId, studyYearId, pageNum-1, out count,limit);
        }

        public void InsertCourse(string name,string courseCode, bool isAlive)
        {
            _dal.InsertCourse(name, courseCode, isAlive);
        }

        public List<Course> GetClassHaveNoCourse(int classId, int studyYear)
        {
            return _dal.GetClassHaveNoCourse(classId,studyYear);
        }

        public bool ClassAddCourse(int classId, int studyYear, int courseId, int teacherId)
        {
            return _dal.ClassAddCourse(classId, studyYear, courseId, teacherId);
        }
    }
}
